<?php


namespace app\user\controller;


use app\http\middleware\checkAdminToken;
use app\http\middleware\checkTTUserToken;
use app\user\logic\LoginMiniProgram;
use app\user\model\TtUser;
use EasyWeChat\Factory;
use think\Controller;
use think\Db;
use think\facade\Config;
use think\facade\Request;

class Login extends Controller
{
    public function login()
    {
        $code = Request::param('code');
        $type = Request::param('platform');     //所属的平台

        $MiniProgramLogin = new LoginMiniProgram($type);

        //调用开放平台的登录
        $info = $MiniProgramLogin->login($code);

        try{
            //调用本系统的登录
            $res = $MiniProgramLogin->miniProLogin($type, $info->openid);

        }catch (\Exception $e){
            $this->result($info, 200, '成功');
        }

        //调用
        $this->result($res, 200, '成功');
    }

    //返回字节跳动在本平台登录的token
    private function get_tt_token($openid)
    {

        $user = TtUser::where(['openid' => $openid])->find();

        $data = [
            "token" => checkTTUserToken::issueToken($user)
        ];
        return $data;
    }

    //创建token
    public function createToken()
    {
        $user_id = Request::param('user_id',17098);
        $user = Db::table('user')->where('id',$user_id)->find();
        $token = checkTTUserToken::issueToken($user);
        return $token;
    }

    //头条登录
    public function ttlogin()
    {
        $code = Request::param('code');
        $user_id = Request::param('user_id', 0);       //用户的id
        $platform = Request::param('platform', 'dy');       //用户的id


        $appid = Config::get('miniprogram.tt.appid');
        $secret = Config::get('miniprogram.tt.secret');

        //平台的api接口
        $api = "http://developer.toutiao.com/api/apps/jscode2session?appid={$appid}&secret={$secret}&code={$code}";
        $info = json_decode(httpGet($api));

        //字节平台的openid
        $openid = $info->openid;

        //判断头条账号是否已经被绑定过了
        $ttuser = TtUser::where(['openid' => $openid])->find();

        //已经绑定过了
        if ($ttuser) {

            //已经和该微信号绑定过了
            if ($ttuser['user_id'] == $user_id) {
                $this->result($this->get_tt_token($openid), 200, '该账号已经绑定过了');
            }
            //绑定了其他的微信，且绑定的id不为0
            if ($ttuser['user_id'] != $user_id && $ttuser['user_id'] != 0) {
                $this->result($this->get_tt_token($openid), 400, '该账号已经绑定了微信了，是否要换绑？');
            }

        } else {
            //还没绑定过呢
            //添加绑定
            $res = TtUser::create([
                'user_id' => $user_id,
                'platform' => 'dy',
                'openid' => $openid,
                'add_time' => time()
            ]);
            if($res){
                $this->result($this->get_tt_token($openid), 200, '绑定成功');
            }else{
                $this->result(null, 300, '登录失败，请重试');
            }
        }


    }


}
